#include "BinarySearch.h"

using namespace std;


/*从list[]中查找toSearch*/
int BinarySearch(int toSearch, int list[], int arrayLen)              
{
	int low,high,mid;
	low = 0;
	high = arrayLen - 1;

	while(low <= high)
	{
		mid = low + (high - low) / 2;               /*折半*/

		if(toSearch < list[mid])
		{
			/*在前半部分查找*/
			high = mid - 1;
		}
		else if(toSearch > list[mid])
		{
			/*在后半部分查找*/
			low = mid + 1;
		}
	   	else
		{
			return mid;
		}
	}
	return(-1);
}
